import api from './index'

// 获取权限列表
export const getPermissions = (params?: { page?: number; page_size?: number }) => {
  return api.get('/permission', { params })
}

// 获取权限树结构
export const getPermissionTree = () => {
  return api.get('/permission/tree')
}

// 获取权限详情
export const getPermissionById = (id: number) => {
  return api.get(`/permission/${id}`)
}

// 根据code获取权限
export const getPermissionByCode = (code: string) => {
  return api.get(`/permission/code/${code}`)
}

// 创建权限
export const createPermission = (data: any) => {
  return api.post('/permission', data)
}

// 更新权限
export const updatePermission = (id: number, data: any) => {
  return api.put(`/permission/${id}`, data)
}

// 删除权限
export const deletePermission = (id: number) => {
  return api.delete(`/permission/${id}`)
}

// 获取角色的权限
export const getRolePermissions = (id: number) => {
  return api.get(`/permission/role/${id}`)
}

// 获取管理员的权限
export const getAdminPermissions = (id: number) => {
  return api.get(`/permission/admin/${id}`)
}

// 获取拥有特定权限的角色
export const getPermissionRoles = (id: number) => {
  return api.get(`/permission/permission/${id}/roles`)
}

// 分配权限给角色
export const assignPermissionToRole = (role_id: number, permission_id: number) => {
  return api.post('/permission/assign', null, {
    params: { role_id, permission_id }
  })
}

// 移除角色的权限
export const removePermissionFromRole = (role_id: number, permission_id: number) => {
  return api.delete('/permission/remove', {
    params: { role_id, permission_id }
  })
}